﻿using SimpleTerminal.Common;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace SimpleTerminal
{
    public partial class ConnectInfo : Form
    {

        //public event EventHandler<SaveProfileEventArgs> Save;

        bool isEdit = false;
        int id = -1;
        public ConnectInfo()
        {
            InitializeComponent();
        }
        public ConnectInfo(int id) : this()
        {
            isEdit = true;
            this.id = id;
            using (ProfileModel profileModel = new ProfileModel())
            {
                ProfileInfo profileInfo = profileModel.Connection.Find<ProfileInfo>(id);
                FillData(profileInfo);
            }
        }


        public void Error(string text)
        {
            MessageBox.Show(text, "保存出错");
        }



        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private ProfileInfo GetProfile()
        {
            ProfileInfo profile = new ProfileInfo
            {
                Name = this.connectName.Text
            };
            string protocolStr = this.connectProtocol.SelectedText;
            string authTypeStr = this.connectAuthType.SelectedText;
            if ("SSH2".Equals(protocolStr))
            {
                profile.Protocol = "SSH2";
            }


            if ("Password".Equals(authTypeStr))
            {
                profile.AuthType = "Password";
            }


            profile.Port = (int)this.connectPort.Value;
            profile.Host = this.connectHost.Text;
            profile.Account = this.connectAccount.Text;
            profile.Password = this.connectPassword.Text;

            if (profile.Name.Length == 0)
            {
                Windows.Error("请输入 连接名称");
                return null;
            }
            if (profile.Host.Length == 0)
            {
                Windows.Error("请添加 连接主机");
                return null;
            }

            return profile;
        }


        private void FillData(ProfileInfo profileInfo)
        {
            this.connectName.Text = profileInfo.Name;
            this.connectProtocol.Text = profileInfo.Protocol;
            this.connectAuthType.Text = profileInfo.AuthType;
            this.connectHost.Text = profileInfo.Host;
            this.connectAccount.Text = profileInfo.Account;
            this.connectPassword.Text = profileInfo.Password;
            this.connectPort.Text = profileInfo.Port.ToString();
        }



        private void BtnOk_Click(object sender, EventArgs e)
        {
            ProfileInfo profile = GetProfile();
            if (profile == null)
                return;

            this.Close();
        }

        
        private bool CheckName(string name, int id)
        {

            using (ProfileModel profileModel = new ProfileModel())
            {
                
                List<ProfileInfo> profiles = profileModel.Connection.Query<ProfileInfo>("select * from ProfileInfo where name = ? and id <> ? limit 1", new object[2] { name, id });
                return profiles.Count > 0;
            }
        }
        private void BtnSave_Click(object sender, EventArgs e)
        {
            ProfileInfo profile = GetProfile();
            if (profile == null)
                return;

            using (ProfileModel profileModel = new ProfileModel())
            {
                if (isEdit)
                {
                    profile.Id = id;
                    if (CheckName(profile.Name, id))
                    {
                        Windows.Error("名字重复，请更换一个名字！");
                        return;
                    }
                    profileModel.Connection.Update(profile, typeof(ProfileInfo));
                }
                else
                {
                    if (CheckName(profile.Name, 0))
                    {
                        Windows.Error("名字重复，请更换一个名字！");
                        return;
                    }
                    profileModel.Connection.Insert(profile,typeof(ProfileInfo));
                }
            }
            this.DialogResult = DialogResult.OK;
            Close();
        }

        private void TreeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
        }
    }
}
